var useFiles = [];

useFiles.push('uikit');
useFiles.push('uikit.css');
useFiles.push('easyui');
useFiles.push('easyui.css');
useFiles.push('easyui-icon.css');
useFiles.push('page.css');
useFiles.push('easyui-locale');

seajs.use(useFiles, function() {
	$(initPage);
});

function initPage() {
	$('a[href="#ok"]').on('click', ok_click);

	$('a[href="#reset"]').on('click', reset_click);

	$('a[href="#back"]').on('click', back_click);
}

function ok_click() {
	queryParams['domainNa.siteId'] = $('select[name="domainNa.siteId"]').val();
	queryParams['domainNa.code'] = $('input[name="domainNa.code"]').val();

	$('.easyui-datagrid').datagrid('reload');

	back_click();
}

function reset_click() {
	$('form').each(function() {
		this.reset();
	});
}

function back_click() {
	$windowForSearch = $('#windowForSearch');

	$windowForSearch.window('close');
}

function onDblClickRow(rowIndex, rowData) {
	var src = fand.webContextUrl + '/admin/configuration/site/domainNa/view.htm?id=' + rowData.ID;

	$windowForIframe = $('#windowForIframe');

	$windowForIframe.find('iframe').prop('src', src);

	$windowForIframe.window({
		title : '查看',
		width : ($('html').width() * 0.95),
		height : ($('html').height() * 0.95),
		modal : true
	});
}

var queryParams = {};

function onBeforeLoad(param) {
	param['domainNa.paginationNo'] = param.page;
	param['domainNa.paginationSize'] = param.rows;

	$.extend(param, queryParams);
}

function add_handler() {
	var src = fand.webContextUrl + '/admin/configuration/site/domainNa/new.htm';

	$windowForIframe = $('#windowForIframe');

	$windowForIframe.find('iframe').prop('src', src);

	$windowForIframe.window({
		title : '新增',
		width : ($('html').width() * 0.95),
		height : ($('html').height() * 0.95),
		modal : true
	});

	return false;
}

function edit_handler() {
	var row = $('.easyui-datagrid').datagrid('getSelected');

	if (row == null) {
		$.messager.alert('提示消息', '请选择需编辑的数据！', 'warning');

		return false;
	}

	var src = fand.webContextUrl + '/admin/configuration/site/domainNa/edit.htm?id=' + row.ID;

	$windowForIframe = $('#windowForIframe');

	$windowForIframe.find('iframe').prop('src', src);

	$windowForIframe.window({
		title : '编辑',
		width : ($('html').width() * 0.95),
		height : ($('html').height() * 0.95),
		modal : true
	});

	return false;
}

function remove_handler() {
	var rows = $('.easyui-datagrid').datagrid('getChecked');

	if (rows.length < 1) {
		$.messager.alert('提示消息', '请勾选需删除的数据！', 'warning');

		return false;
	}

	$.messager.confirm('确认', '将删除选择的数据?', function(r) {
		if (r) {
			var ids = [];

			$(rows).each(function(i, row) {
				ids.push(row.ID);
			});

			var url = fand.webContextUrl + '/admin/configuration/site/domainNa/delete.json';

			$.messager.progress();

			$.post(url, {
				ids : ids.join(',')
			}, function(data) {
				$.messager.progress('close');

				if (data.message == undefined) {
					$.messager.alert('提示消息', '删除成功！', 'info');

					$('.easyui-datagrid').datagrid('reload');

					return;
				}

				$.messager.alert('提示消息', '删除失败:' + data.message, 'error');
			});
		}
	});

	return false;
}

function view_handler() {
	var row = $('.easyui-datagrid').datagrid('getSelected');

	if (row == null) {
		$.messager.alert('提示消息', '请选择需查看的数据！', 'warning');

		return false;
	}

	onDblClickRow((row.RN - 1), row);
}

function search_handler() {
	$windowForSearch = $('#windowForSearch');

	$windowForSearch.window({
		width : ($('html').width() * 0.5),
		height : ($('html').height() * 0.5),
		modal : true
	});

	return false;
}

var toolbar = [ {
	text : '新增',
	iconCls : 'icon-add',
	handler : add_handler
}, {
	text : '编辑',
	iconCls : 'icon-edit',
	handler : edit_handler
}, '-', {
	text : '批量删除',
	iconCls : 'icon-remove',
	handler : remove_handler
}, '-', {
	text : '查看',
	iconCls : 'icon-tip',
	handler : view_handler
}, '-', {
	text : '查询',
	iconCls : 'icon-search',
	handler : search_handler
} ];